var request = require("../../../utils/mr_xu/request.js")
var api = require("../../../utils/mr_xu/api_url.js").api
var orderCache = require('../../../utils/mr_xu/apply_order_cache.js')
var common = require('../../../utils/mr_xu/common.js')


function check_licensePlate(licensePlate) {

  var re = /^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}(([A-HJ-Z]{1}[A-HJ-NP-Z0-9]{5})|([A-HJ-Z]{1}(([DF]{1}[A-HJ-NP-Z0-9]{1}[0-9]{4})|([0-9]{5}[DF]{1})))|([A-HJ-Z]{1}[A-D0-9]{1}[0-9]{3}警)))|([0-9]{6}使)|((([沪粤川云桂鄂陕蒙藏黑辽渝]{1}A)|鲁B|闽D|蒙E|蒙H)[0-9]{4}领)|(WJ[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼·•]{1}[0-9]{4}[TDSHBXJ0-9]{1})|([VKHBSLJNGCE]{1}[A-DJ-PR-TVY]{1}[0-9]{5})&/

  if (licensePlate.search(re) == -1) {
    return false;
  } else {
    return true;
  }
}

Page({

  data: {
    show_key_board: 0,
    key_board_type: 1,
    car_type: [{
      value: 1,
      name: ''
    }],
    car_type_index: 1,
    car_board_province: "川",
    car_board_number: "",

    frameNo: "",
    engineNo: "",
    carImageList: [],
    brand:'',
    vehicle:''//车系
  },

  toggle_board_province: function() {
    let now = !this.data.show_key_board
    this.setData({
      show_key_board: now,
      key_board_type: 1
    })
  },
  imagesChange(e) {
   
    let images = e.detail
    this.setData({
      carImageList: images
    })
    
  },
  close_key_board: function() {
    this.setData({
      show_key_board: 0
    })
  },
  key_board_delete: function() {
    
    let number = this.data.car_board_number

    if (number.length == 0)
      return

    number = number.substring(0, number.length - 1)

    this.setData({
      car_board_number: number
    })
  },
  toggle_board_number: function() {
    let now = !this.data.show_key_board
    this.setData({
      show_key_board: now,
      key_board_type: 0
    })
    console.log(this.data.show_key_board)
  },
  key_input_change: function(res) {
    if (this.data.key_board_type == 1) {
      this.setData({
        car_board_province: res.detail,
        show_key_board: 0
      })
    } else {

      if (typeof res.detail == 'undefined')
        return  

      let number = this.data.car_board_number

      if (number.length > 6)
        return

      number = number + res.detail
      this.setData({
        car_board_number: number,
        show_key_board: 1
      })

      if (number.length == 6) {
        this.close_key_board()
        return
      }
    }

  },
  bindPickerChange: function(res) {
    this.setData({
      car_type_index: res.detail.value
    })
  },
  onLoad: function(options) {
    
    let that = this
    request.post(api.car_type_list, {
      data: "car_type"
    }, function(res) {
      that.setData({
        car_type: res.obj
      })

      let updateInfo = orderCache.getUpdateInfo()
      if (updateInfo) {
        console.log(updateInfo)
        let index = 1
        for (let i in res.obj) {
          if (res.obj[i].value == updateInfo.car.type) {
            index = i
            break
          }
        }
        let car = updateInfo.car
        that.setData({
          car_type_index: index,
          car_board_province:car.carNo.substring(0,1),
          car_board_number: car.carNo.substring(1),
          frameNo: car.frameNo,
          engineNo: car.engineNo,
          remark: updateInfo.remark,
          carImageList: car.carImageLst ? car.carImageLst : []
        })
      } else {
        that.setData({
          carImageList:[]
        })
      }
    })
  },
  scan() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {

        var path = res.tempFilePaths[0]
        common.scanCertificate(path, function (engineNo, frameNo, carNo) {
          let car_board_province = carNo.substring(0,1)
          let car_board_number = carNo.substring(1)
          that.setData({
            car_board_province: car_board_province,
            car_board_number: car_board_number,
            engineNo: engineNo,
            frameNo: frameNo
          })
        })
      },
    })
  },
  edit_remark: function(res) {
    this.setData({
      remark: res.detail.value
    })
  },
  edit_vehicle: function (res) {
    this.setData({
      vehicle: res.detail.value
    })
  },
  edit_engineNo: function (evt) {
    let value = evt.detail.value.toUpperCase().substring(0,25)
    this.setData({
      engineNo: value
    })
  },
  edit_brand(res) {
    this.setData({
      brand: res.detail.value
    })
  },
  edit_frameNo: function (evt) {
    let value = evt.detail.value.toUpperCase().substring(0, 25)
    this.setData({
      frameNo: value
    })
  },
  
  next: function() {

    let that = this

    common.safeRun(function() {

      if (check_licensePlate(that.data.car_board_province + that.data.car_board_number) == false)
        throw new Error("请输入正确的车牌")

      // if (!/^[0-9a-zA-Z]{15,20}$/.test(that.data.frameNo))
      //   throw new Error("请输入正确的车架号") 

      // if (!/^[0-9a-zA-Z]{5,20}$/.test(that.data.engineNo))
      //   throw new Error("请输入发动机号")    

      if (that.data.brand == '')
        throw new Error("请输入正确汽车品牌")    

      if (that.data.vehicle == '')
        throw new Error("请输入汽车车系")      

      orderCache.setItem('remark', that.data.remark)
      orderCache.setItem('car', {
        carNo: that.data.car_board_province + that.data.car_board_number,
        type: that.data.car_type[that.data.car_type_index].value,
        frameNo: that.data.frameNo,
        engineNo: that.data.engineNo,
        brand: '',
        isGps: false,
        carImageLst: that.data.carImageList,
        brand:that.data.brand,
        vehicle: that.data.vehicle
      })
      wx.navigateTo({
        url: '../step3_car_gps/car_gps',
      })
    })
  }
})